package com.lifeverse.dto;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.lifeverse.entity.ConsciousnessState;
import lombok.Data;

import java.math.BigDecimal;
import java.time.LocalDateTime;

/**
 * 意识状态数据传输对象
 */
@Data
public class ConsciousnessStateDTO {
    
    private Long id;
    
    private Long lifeEntityId;
    
    private String lifeEntityName;
    
    private ConsciousnessState.StateType stateType;
    
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime stateTimestamp;
    
    private BigDecimal intensityLevel;
    
    private String attentionFocus;
    
    private String emotionalState;
    
    private BigDecimal cognitiveLoad;
    
    private BigDecimal creativityIndex;
    
    private BigDecimal logicClarity;
    
    private BigDecimal intuitionSensitivity;
    
    private BigDecimal socialActivity;
    
    private BigDecimal learningEfficiency;
    
    private BigDecimal memoryActivity;
    
    private BigDecimal decisionCapability;
    
    private Long durationSeconds;
    
    private String triggerFactor;
    
    private String outcomeDescription;
    
    private String relatedEntityIds;
    
    private String stateData;
    
    private BigDecimal qualityScore;
    
    private Boolean isAnomaly;
    
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime endTimestamp;
    
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime createdAt;
    
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime updatedAt;
    
    /**
     * 计算状态的综合评分
     */
    public BigDecimal calculateOverallScore() {
        if (intensityLevel == null || creativityIndex == null || logicClarity == null || 
            intuitionSensitivity == null || socialActivity == null || learningEfficiency == null ||
            memoryActivity == null || decisionCapability == null) {
            return BigDecimal.ZERO;
        }
        
        return intensityLevel
                .add(creativityIndex)
                .add(logicClarity)
                .add(intuitionSensitivity)
                .add(socialActivity)
                .add(learningEfficiency)
                .add(memoryActivity)
                .add(decisionCapability)
                .divide(BigDecimal.valueOf(8), 2, java.math.RoundingMode.HALF_UP);
    }
    
    /**
     * 判断状态是否活跃
     */
    public boolean isActive() {
        return endTimestamp == null && intensityLevel != null && intensityLevel.compareTo(BigDecimal.valueOf(10)) > 0;
    }
    
    /**
     * 判断状态是否高效
     */
    public boolean isEfficient() {
        return qualityScore != null && qualityScore.compareTo(BigDecimal.valueOf(70)) >= 0;
    }
    
    /**
     * 获取状态持续时间（分钟）
     */
    public Long getDurationMinutes() {
        if (durationSeconds != null) {
            return durationSeconds / 60;
        }
        return null;
    }
    
    /**
     * 判断是否为长时间状态（超过1小时）
     */
    public boolean isLongDuration() {
        return durationSeconds != null && durationSeconds > 3600;
    }
    
    /**
     * 获取状态类型描述
     */
    public String getStateTypeDescription() {
        return stateType != null ? stateType.getDescription() : null;
    }
}